//
//  PKReadMainListCarouseView.m
//  PianKe
//
//  Created by lanou on 15/10/9.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "PKReadMainListCarouseView.h"
#import <UIImageView+WebCache.h>
#import "PKReadMainCarouselModel.h"
@interface PKReadMainListCarouseView()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *scrollerView;
@property(nonatomic,strong)UIImageView *pictureImg;
@property(nonatomic,strong)UIPageControl *pageControl;
@end
@implementation PKReadMainListCarouseView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        _scrollerView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
        _scrollerView.contentSize=CGSizeMake(SCREEN_WIDTH*4, 0);
        _scrollerView.pagingEnabled=YES;
        _scrollerView.delegate=self;
#warning 设置水平方向能否滑动
        _scrollerView.showsHorizontalScrollIndicator=NO;
        for (int i=0; i<3; i++) {
            _pictureImg=[[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, 120)];
            _pictureImg.userInteractionEnabled=YES;
            [_scrollerView addSubview:_pictureImg];
        }
       
#warning 创建一个轮播的占位图片
        UIImageView *tempIamgeView=[[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH *3, 0, SCREEN_WIDTH, 120)];
        [_scrollerView addSubview:tempIamgeView];
         [self addSubview:_scrollerView];
        _pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(280, 90, 60, 20)];
        _pageControl.numberOfPages=3;
#warning 未被选中的颜色设置为灰色
        _pageControl.pageIndicatorTintColor=[UIColor grayColor];
        _pageControl.currentPageIndicatorTintColor=[UIColor greenColor];
        [self addSubview:_pageControl];
        
    }
    return self;
}
-(void)setCarouseArray:(NSArray *)CarouseArray
{
    _CarouseArray=CarouseArray;
    for (int i=0; i<CarouseArray.count+1; i++) {
        UIImageView *imageView=_scrollerView.subviews[i];
        if (i<CarouseArray.count) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:((PKReadMainCarouselModel *)CarouseArray[i]).img]];
            UITapGestureRecognizer *tapRecognized=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(skipToReadPage:)];
            imageView.tag=1000+i;
            [imageView addGestureRecognizer:tapRecognized];
            
//            
        }
        else
        {
            [imageView sd_setImageWithURL:[NSURL URLWithString:((PKReadMainCarouselModel *)CarouseArray[0]).img]];
        }
    }
}

-(void)skipToReadPage:(UIGestureRecognizer *)recognized
{
    NSInteger count=recognized.view.tag-1000;
    self.tapCarouselBlock(count);
    
}

#warning ScrollerView停止减速时，PageControl跳转到对应的currentPage
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //当前pageController的选中的下标等于轮播图的偏移量处于SCroller的显示界面的宽度,当偏移量为最后一个的第四张图片的偏移量的时候，当前选中的下标为0
    if (scrollView.contentOffset.x==SCREEN_WIDTH *3) {
        _pageControl.currentPage=0;
    }
    else{
    _pageControl.currentPage=scrollView.contentOffset.x/SCREEN_WIDTH;
    }
    
}
#warning 只要滑动轮播图就会一直执行这个方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//判断scrollerView的偏移量
    //如果偏移量大于scrollerView.contentSize.with,说明是向右滑动，则将偏移量设置成（0，0）此时最后一张图片转化成第一张图片，实现向右轮播的效果
    //如果偏移量小于0，说明是向左滑动，则将偏移量设置成最后一张图时的偏移量，如：(SCREEN_WIDTH*3,0)此时第一张图片会转化成最后一张图片，实现向左轮播的效果
    if (_scrollerView.contentOffset.x+SCREEN_WIDTH>_scrollerView.contentSize.width) {
        _scrollerView.contentOffset=CGPointMake(0, 0);
    }
    else if (_scrollerView.contentOffset.x<0)
    {
        _scrollerView.contentOffset=CGPointMake(SCREEN_WIDTH *3, 0);
    }
}
@end
